#ifndef __MATRIX_H
#define __MATRIX_H
#include<iostream>

//矩阵模板类（public更容易）
template<typename T> class Matrix{
public:
    T* matrix_elements;
    int row;
    int column;
    Matrix(){
	matrix_elements=NULL;
    row=0;
	column=0;
    }
//输入矩阵
    Matrix(int m,int n)
    {
	matrix_elements = new T[m*n];
	row=m;
	column=n;
	for(int i=0;i<m;i++)
	    for(int j=0;j<n;j++)
		std::cin>>matrix_elements[i*n+j];
    }
//析构函数
    ~Matrix(){
	if(matrix_elements != NULL)
	delete matrix_elements;
    }
//判断方阵的函数
    bool is_square(){
	if(n_column==n_row)
	    return true;
	else
	    return false;
    }
//矩阵“=”的函数
    Matrix<T>& operator = (const Matrix<T>& a)
    {
	column= a.column;
	row = a.row;
	if(matrix_elements != NULL)
	    delete [] matrix_elements;
	matrix_elements = new T[column*row];
	for(int i=0;i<row;i++)
	    for(int j=0;j<column;j++)
		matrix_elements[i*column+j] = a.matrix_elements[i*column+j];
	return *this;
    }
//矩阵“*”的函数
    Matrix<T> operator * (const Matrix<T>& a)
    {
	static Matrix<T> b;
	if(column!=a.row)
	{
	    std::cout<<"false operation"<<std::endl;
	    return b;
	}
	b.row=row;
	b.column=a.column;
	b.matrix_elements = new T[b.row*b.column];
	for(int i=0 ;i<b.row;i++)
	    for(int j=0;j<b.column;j++)
	    {
		    b.matrix_elements[i*b.column+j]=0;
		    for(int k=0; k<column;k++){
		    b.matrix_elements[i*b.column+j]+=matrix_elements[i*column+k]*a.matrix_elements[k*a.column+j];  
		    }
	    }
    return b;		    
    }
//打印矩阵的函数
    void print()
    {
	    for(int i=0;i<row;i++){
	        for(int j=0;j<column;j++)
		    std::cout<<matrix_elements[i*column+j]<<' ';
	        std::cout<<'\n';
	    }
    }
};

//行向量模板类
template<typename T> class RowVector:public Matrix<T>{
public:
    RowVector():Matrix<T>(){}
    RowVector(int n):Matrix<T>(1,n){}
    friend T operator *(const RowVector& a, const RowVector& b){
	if(a.column!=b.column)
	{
	    std::cout<<"false operation"<<std::endl;
	    return 0;
	}
	T ret=0;
	for(int i=0;i<a.column;i++)
	    ret += a.matrix_elements[i] * b.matrix_elements[i];
	return ret;
    }
};

//列向量模板类
template<typename T> class ColVector:public Matrix<T>
{
public:
    ColVector():Matrix<T>(){}
    ColVector(int n):Matrix<T>(n,1){}
    friend T operator *(const ColVector& a, const ColVector& b){
	if(a.column!=b.column)
	{
	    std::cout<<"false operation"<<std::endl;
	    return 0;
	}
	T ret=0;
	for(int i=0;i<a.row;i++)
	    ret += a.matrix_elements[i] * b.matrix_elements[i];
	return ret;
    }
};

//列向量乘行向量的函数
template <typename T> T operator * (const RowVector<T>& a,const ColVector<T>& b)
{
    if(a.n_column!=b.n_row){
	std::cout<<"false operation"<<std::endl;
	return 0;
    }
    T ret=0;
    for(int i=0;i<a.n_column;i++)
	ret+=a.matrix_elements[i]*b.matrix_elements[i];
    return ret;
}

#else
//DO NOTHING
#endif